package com.woohua.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.woohua.demo.domain.Order;
import com.woohua.demo.dto.OrderDTO;
import com.woohua.demo.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/***
 * @title OrderController
 * @description
 * @author baiji
 * @version 1.0.0
 * @create 2023/11/14 15:56
 **/
@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("add")
    public String add() {
        orderService.add();

        return "添加成功, date: " + LocalDateTime.now();
    }

    @PostMapping("list")
    public List<Order> list(@RequestBody OrderDTO dto) {
        HashMap<String, Object> map = new HashMap<>(4);
        if (null != dto.getOrderId()) {
            map.put("order_id", dto.getOrderId());
        }
        if (StringUtils.isNotEmpty(dto.getOrderNo())) {
            map.put("order_no", dto.getOrderNo());
        }
        if (null != dto.getOrderType()) {
            map.put("order_type", dto.getOrderType());
        }
        if (null != dto.getUserId()) {
            map.put("user_id", dto.getUserId());
        }
        return orderService.listByMap(map);
    }

    @GetMapping("count")
    public Long count() {
        return orderService.count();
    }
}
